string author = "Live";
string pluginName = "test 测试插件";
string version = "1.0";

void OnPluginInit()
{
    // 设置插件信息
    PluginData::SetAuthor(author);
    PluginData::SetName(pluginName);
    PluginData::SetVersion(version);

    // 加载后提示信息
    ThePresident.InfoFeed("插件:"+pluginName+"| 版本:"+version+"| 作者:"+author,false);
    Events::Infected::OnInfectedSpawned.Hook( @OnInfectedSpawned );
    Events::Infected::OnInfectedKilled.Hook( @OnInfectedKilled );
    Events::FlatLine::WaveStart.Hook( @WaveStart );
    Events::FlatLine::WaveEnded.Hook( @WaveEnded );
    Events::Player::PlayerSay.Hook( @PlayerSay );
    Events::Player::OnMenuExecuted.Hook( @OnMenuExecuted );
    Events::Player::OnPlayerSpawn.Hook( @OnPlayerSpawn );
    Events::ThePresident::OnTerminateRound.Hook( @OnTerminateRound );
	Events::Player::OnPlayerDamagedPre.Hook( @OnPlayerDamagedPre );
	ThePresident_OnMapStart();
}

HookReturnCode OnPlayerSpawn(CTerrorPlayer@ pPlayer)
{
    if ( pPlayer.GetTeamNumber() == TEAM_SURVIVOR ){
        pPlayer.GiveWeapon( "ar15" );
    }
    return HOOK_CONTINUE;
}

void ThePresident_OnMapStart()
{
    ThePresident_OnRoundStart();
}

void ThePresident_OnRoundStart()
{
	Engine.RunConsoleCommand("difficulty 4");
    Constant_ZOMBIE = 0;
    Constant_CARRIER = 0;
    Constant_CHARGER = 0;
    Constant_RIOT = 0;
    Constant_SCREAMER = 0;
    Constant_SCREAMER_BRIDE = 0;
    ThePresident.BeginHordeMode();
    Chat.PrintToChat(all,"锁人已启动");
	Engine.RunConsoleCommand("hostname [CN.Live] 防守 加强版0.1测试服 flatline");
    Engine.RunConsoleCommand( "mapcyclefile flatline.txt");
}

HookReturnCode PlayerSay(CTerrorPlayer@ pPlayer, CASCommand@ pArgs)
{
    string arg = pArgs.Arg( 1 );
	string arg1 = "";
	string arg2 = "";

	// 切分字符串(这样就可以用空格传一个命令参数了)
	int subIndex = arg.findFirst(" ",0);
	if(not (subIndex < 0)){
		arg1 = arg.substr(0,subIndex);
		arg2 = arg.substr(subIndex+1,-1);
		//Chat.PrintToChat(all,""+arg1+"|"+arg2);
	}else{
		arg1 = arg;
	}

    if(arg1 == "!ent"){
        if(arg2 != ""){
            EntityCreator::Create(arg2, pPlayer.EyePosition(),pPlayer.EyeAngles());
        }
    }
/*     if(arg1 == "!zc"){
        if(arg2 != ""){
            ThePresident.SetSpawnConstantFlags(ZSF_FORCED_AREA);
            ThePresident.SetSpawnZombiesConstant( Utils.StringToInt(arg2));
        }
    } */
    if(arg1 == "!v"){
        Vector v = pPlayer.GetLocalOrigin();
        Chat.PrintToChat(all,""+v.x+"|"+v.y+"|"+v.z);
    }else if( arg1 == "!b" ){
        SHOP(pPlayer);
    }else if( arg1 == "!fh"){
		// 修改被复活玩家的游戏阵营
		pPlayer.ChangeTeam( TEAM_SURVIVOR );
		// 复活玩家
		pPlayer.Respawn();
	}else if ( arg1 == "!give"){
		if( AdminSystem.PlayerHasAccessLevel(pPlayer,LEVEL_SUPERADMIN )){
			pPlayer.GiveWeapon(arg2);
		}
	}else if(arg1 == "!on"){
		Wave = true;
		Chat.PrintToChat(all,"手动启动波");
	}else if( arg1 == "!re" ){
		ZOMBIE_Num = 0;
		Chat.PrintToChat(all,"普通僵尸数量重置");
	}else if( arg1 == "!sel" ){
		Chat.PrintToChat(all,"CARRIER "+CARRIER_Num+" | ZOMBIE "+ZOMBIE_Num+" | CHARGER_Num "+CHARGER_Num+" | RIOT_Num "+RIOT_Num+" | SCREAMER_Num"+SCREAMER_Num+" | SCREAMER_BRIDE_Num"+SCREAMER_BRIDE_Num);
	}else if( arg1 == "!cl" ){
		// 清除所有子弹
		string ammo = "item_ammo_*";
		CBaseEntity @pEnt = FindEntityByClassname( null, ammo );
		while( pEnt !is null)
		{
			pEnt.SUB_Remove();
			@pEnt = FindEntityByClassname( pEnt, ammo );
		}
		Chat.PrintToChat(all,"清除所有子弹");
        string name = "item_upgrademodule";
        CBaseEntity @base = FindEntityByClassname( null, name );
		while( base !is null)
		{
			base.SUB_Remove();
			@base = FindEntityByClassname( base, name );
		}
		Chat.PrintToChat(all,"清除配件");
	}
    return HOOK_CONTINUE;
}

HookReturnCode OnPlayerDamagedPre(CTerrorPlayer@ pPlayer, CTakeDamageInfo &in DamageInfo)
{
	CBaseEntity@ cWeapon = DamageInfo.GetWeapon();
	if( cWeapon.ClassnameIs("weapon_grenadelauncher")){
		DamageInfo.SetDamage(0);
	}
	return HOOK_CONTINUE;
}

array<int> money = {200,200,200,200,200,200,0,0,0,0,0,0,0,0,0,0};

void SHOP(CTerrorPlayer@ pPlayer){
    Menu pMenu;
    pMenu.SetID("SHOP");
    pMenu.SetTitle("商店");
    pMenu.AddItem("无限火力60秒");
    pMenu.Display(pPlayer,-1);
    Chat.PrintToChat(pPlayer,"当前金额：{gold}"+money[pPlayer.entindex()-1]);
}

HookReturnCode OnMenuExecuted(CTerrorPlayer@ pPlayer, const string &in szID, int &in iValue)
{
    if( szID == "SHOP" ){
        switch( iValue ){
            case 1:{
                if( !openMAXAMMO ){
                    if( money[pPlayer.entindex()-1] > 100 ){
                        money[pPlayer.entindex()-1] -= 100;
                        MAXAMMO();
                        Chat.PrintToChat(all,"{green} "+pPlayer.GetPlayerName()+"{red}购买了无限火力60秒");
                    }else{
                        Chat.PrintToChat(pPlayer,"金币不够");
                    }
                }else{
                    Chat.PrintToChat(pPlayer,"{red}已经有人购买了无限火力");
                }
            }
        }
    }
	return HOOK_CONTINUE;
}

bool openMAXAMMO = false;

void MAXAMMO(){
    CASConVarRef@ sv_infinite_ammo = ConVar::Find( "sv_infinite_ammo" );
    sv_infinite_ammo.SetValue("1");
    openMAXAMMO = true;
    Schedule::Task( 60.0, "closeMAXAMMO" );
}

void closeMAXAMMO(){
    CASConVarRef@ sv_infinite_ammo = ConVar::Find( "sv_infinite_ammo" );
    sv_infinite_ammo.SetValue("0");
    openMAXAMMO = false;
}

// 波数进行中？
bool Wave = false;

// 当前僵尸数
int ZOMBIE_Num = 0;
// 当前黄裤子boss数
int CARRIER_Num = 0;
// 当前盾牌boss数
int CHARGER_Num = 0;
// 当前防爆僵尸数
int RIOT_Num = 0;
// 当前尖叫僵尸数
int SCREAMER_Num = 0;
// 当前尖叫新娘数
int SCREAMER_BRIDE_Num = 0;

// 常量
int Constant_ZOMBIE = 0;
int Constant_CARRIER = 0;
int Constant_CHARGER = 0;
int Constant_RIOT = 0;
int Constant_SCREAMER = 0;
int Constant_SCREAMER_BRIDE = 0;


HookReturnCode WaveStart(int &in iRound)
{
    if( iRound == 1 ){
        Engine.RunConsoleCommand("cf_zombie_healthincrease_type 2");
        Engine.RunConsoleCommand("cf_enable_bosses 1");
        Engine.RunConsoleCommand("cf_zombie_dealsamedamage 1");
    }
    ThePresident.BeginHordeMode();
    CASConVarRef@ infiniteAmmo= ConVar::Find( "sv_infinite_collected_ammo" );
    infiniteAmmo.SetValue("1");
	Chat.PrintToChat(all,"无限子弹已启用");
	Schedule::Task(2,"infiniteStamina");
    // 当前数量归0
    ZOMBIE_Num = 0;
    CARRIER_Num = 0;
    CHARGER_Num = 0;
    RIOT_Num = 0;
    SCREAMER_Num = 0;
    SCREAMER_BRIDE_Num = 0;
    
    // 增加常量
	if( iRound < 8 )
		Constant_ZOMBIE = 40 + (iRound-2) * 4;
	else
		Constant_ZOMBIE = 80;
    Constant_CARRIER = 0 + (iRound-5) * 2;
    Constant_RIOT = 0 + (iRound - 4) * 3;
    Constant_CHARGER = 0 + (iRound-6) * 3;
    //Constant_SCREAMER = 0 + (iRound-5) * 1;
    //Constant_SCREAMER_BRIDE = 5 + iRound;

    spawnZombie();
    Wave = true;
	return HOOK_CONTINUE;
}

void infiniteStamina(){
	CASConVarRef@ infiniteStamina = ConVar::Find( "sv_infinite_stamina" );
	infiniteStamina.SetValue("1");
	Chat.PrintToChat(all,"无限体力已启用");
}


HookReturnCode WaveEnded(bool &in bCompleted)
{
    Wave = false;
	return HOOK_CONTINUE;
}

void spawnZombie(){
    if( Wave ){
        // Chat.PrintToChat(all,"CARRIER "+CARRIER_Num+" | ZOMBIE "+ZOMBIE_Num+" | CHARGER_Num "+CHARGER_Num+" | RIOT_Num "+RIOT_Num+" | SCREAMER_Num"+SCREAMER_Num+" | SCREAMER_BRIDE_Num"+SCREAMER_BRIDE_Num);
        if( CARRIER_Num < Constant_CARRIER ){
            CBaseEntity@ entCARRIER = EntityCreator::Create("CARRIER", Vector(5858.3,1594.16,-2047.97) , QAngle(0, 0, 0));
            entCARRIER.SetEntityName("carrier");
            CARRIER_Num += 1;
        }
        if ( ZOMBIE_Num < Constant_ZOMBIE ){
            CBaseEntity@ entZOMBIE = EntityCreator::Create("ZOMBIE", Vector(5862.08,811.315,-2047.97) , QAngle(0, 0, 0));
            entZOMBIE.SetEntityName("zombie");
            ZOMBIE_Num += 1;
        }
        if( CHARGER_Num < Constant_CHARGER ){
            CBaseEntity@ entRIOTCHARGER = EntityCreator::Create("RIOTCHARGER", Vector(5858.3,1594.16,-2047.97) , QAngle(0, 0, 0));
            entRIOTCHARGER.SetEntityName("riotcharger");
            CHARGER_Num += 1;
        }
        if( RIOT_Num < Constant_RIOT ){
            CBaseEntity@ entZOMBIERIOT = EntityCreator::Create("ZOMBIERIOT", Vector(5862.08,811.315,-2047.97) , QAngle(0, 0, 0));
            entZOMBIERIOT.SetEntityName("zombieriot");
            RIOT_Num += 1;
        }
        if( SCREAMER_Num < Constant_SCREAMER ){
            CBaseEntity@ entSCREAMER = EntityCreator::Create("SCREAMER", Vector(5807.21,1262.05,-1983.97) , QAngle(0, 0, 0));
            entSCREAMER.SetEntityName("screamer");
            SCREAMER_Num += 1;
        }
        if( SCREAMER_BRIDE_Num < Constant_SCREAMER_BRIDE ){
            CBaseEntity@ entSCREAMERBRIDE = EntityCreator::Create("SCREAMERBRIDE", Vector(5807.21,1262.05,-1983.97) , QAngle(0, 0, 0));
            entSCREAMERBRIDE.SetEntityName("screamerbride");
            SCREAMER_BRIDE_Num += 1;
        }
    }
    Schedule::Task(0.5f, "spawnZombie");
}

HookReturnCode OnInfectedSpawned(Infected@ pInfected)
{
    //pInfected.SetMaxHealth(200000);
    //pInfected.SetHealth(200000);
    //Chat.PrintToChat(all,pInfected.GetZombieID()+"{white}的血量{red}："+pInfected.GetHealth());
	return HOOK_CONTINUE;
}


HookReturnCode OnInfectedKilled(Infected@ pInfected, CTakeDamageInfo &in DamageInfo)
{
    if (pInfected.GetEntityName() == "CARRIER"){
        CARRIER_Num -= 1;
    }else if(pInfected.GetEntityName() == "zombie"){
        ZOMBIE_Num -= 1;
    }else if(pInfected.GetEntityName() == "RIOTCHARGER"){
        CHARGER_Num -= 1;
    }else if(pInfected.GetEntityName() == "ZOMBIERIOT"){
        RIOT_Num -= 1;
    }else if(pInfected.GetEntityName() == "screamer"){
        SCREAMER_Num -= 1;
    }else if(pInfected.GetEntityName() == "screamerbride"){
        SCREAMER_BRIDE_Num -= 1;
    }else if( pInfected.GetEntityName() == "" ){
		ZOMBIE_Num -= 1;
	}else{
        Chat.PrintToChat(all,"异常实体名："+pInfected.GetEntityName());
    }
    CBaseEntity@ pAttacker = DamageInfo.GetAttacker();	//获取攻击者信息
    CTerrorPlayer@ pTerror = ToTerrorPlayer(pAttacker);	//转换攻击者为玩家属性
    money[pTerror.entindex()-1] += 1; 
	return HOOK_CONTINUE;
}

HookReturnCode OnTerminateRound(int iTeam)
{
    Constant_ZOMBIE = 0;
    Constant_CARRIER = 0;
    Constant_CHARGER = 0;
    Constant_RIOT = 0;
    Constant_SCREAMER = 0;
    Constant_SCREAMER_BRIDE = 0;
    money = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	return HOOK_CONTINUE;
}

